package com.tuanzi.loan.business.service.statistics.impl;

import com.tuanzi.loan.business.repository.statistics.LoanAdvertiseTransFormationRepository;
import com.tuanzi.loan.business.service.statistics.LoanAdvertiseTransFormationService;
import com.tuanzi.loan.core.aspect.Debug;
import com.tuanzi.loan.core.vo.PageSearcher;
import com.tuanzi.loan.vo.request.advertise.AdvertiseTransStatRequest;
import com.tuanzi.loan.vo.response.advertise.AdvertiseTransStatResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.retry.RetryException;
import org.springframework.retry.annotation.Retryable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

/**
 * @author Simon
 * @date 2019/07/29 14:29
 */
@Slf4j
@Debug
@Service
@Transactional
@Retryable(value = {RetryException.class})
public class LoanAdvertiseTransFormationServiceImpl implements LoanAdvertiseTransFormationService {

    @Autowired
    private LoanAdvertiseTransFormationRepository loanAdvertiseTransFormationRepository;


    @Override
    public Page<AdvertiseTransStatResponse> list(PageSearcher<AdvertiseTransStatRequest> pageSearcher) {
        if (Objects.isNull(pageSearcher))
            throw new NullPointerException("pageSearcher is null");
        if (Objects.isNull(pageSearcher.getSearchCondition()) || Objects.isNull(pageSearcher.getPageable()))
            throw new NullPointerException("activityparticipatestatSearchRequest or pageable is null");
        return loanAdvertiseTransFormationRepository.list(pageSearcher.getSearchCondition(),pageSearcher.getPageable());
    }

    @Override
    public String getExcel(AdvertiseTransStatRequest request) {
        List<AdvertiseTransStatResponse> list = getDataList(request);
        StringBuilder sf = new StringBuilder();
        sf.append("时间,计划标识,广告平台,手机平台,渠道号,回传数\r\n");
        for (AdvertiseTransStatResponse advertiseTransStatResponse: list) {
            sf.append(advertiseTransStatResponse.getTime()).append(",");
            sf.append(advertiseTransStatResponse.getAdid()).append(",");
            sf.append(advertiseTransStatResponse.getAdsPlatformType()).append(",");
            sf.append(advertiseTransStatResponse.getPlatform()).append(",");
            sf.append(advertiseTransStatResponse.getChannel()).append(",");
            sf.append(advertiseTransStatResponse.getCount()).append(",");
            sf.append("\r\n");
        }
        return sf.toString();
    }
    @Override
    public Long getCountSum() {
        return loanAdvertiseTransFormationRepository.getCountSum();
    }

    public List<AdvertiseTransStatResponse> getDataList(AdvertiseTransStatRequest searchRequest) {
        return loanAdvertiseTransFormationRepository.getDataList(searchRequest);
    }
}
